//
//  ItemGroupFromViewController.m
//  MoneyBook
//
//  Created by Justin Kim on 11. 6. 30..
//  Copyright 2011 Daum. All rights reserved.
//

#import "ItemGroupFormViewController.h"


@implementation ItemGroupFormViewController
@synthesize itemTitle = _itemTitle;
@synthesize isIn = _isIn;
@synthesize itemGroupId = _itemGroupId;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave
                                                                                  target:self
                                                                                  action:@selector(saveAction)];
        [self.navigationItem setRightBarButtonItem:rightBtn];
        [rightBtn release];
    }
    return self;
}

- (void)dealloc{
    [_itemTitle release];
    [_isIn release];
    [_itemGroupId release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning{
    [super didReceiveMemoryWarning];
}

#pragma mark - View lifecycle
- (void)viewDidLoad{
    [super viewDidLoad];
}

- (void)viewDidUnload{
    [self setItemTitle:nil];
    [self setIsIn:nil];
    [self setItemGroupId:nil];
    [super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark -
#pragma mark UIBarButtonItemDelegate
/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)saveAction{
    if(_isCreateMode){
        [[SQLHandler sharedInstance] createItemGroupWithTitle:[_itemTitle text] isIn:[_isIn selectedSegmentIndex]==0?@"out":@"in"];
    }else{
    //    NSLog(@"saveActoin: %d, %@, %d", [_itemGroupId intValue], [_itemTitle text], [_isIn selectedSegmentIndex]);
        [[SQLHandler sharedInstance] updateItemGroupId:_itemGroupId 
                                                 title:[_itemTitle text] 
                                                  isin:[_isIn selectedSegmentIndex]==0?@"out":@"in"];
    }
    [[self navigationController] popViewControllerAnimated:YES];
}
#pragma mark -
#pragma mark user methods
/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)loadItemGroup:(NSNumber*)itemGroupId{
    _isCreateMode = NO;
    NSDictionary *dic = [[SQLHandler sharedInstance] findItemGroupById:itemGroupId];
//    NSLog(@"dic:%@",dic);
    self.itemGroupId = itemGroupId;
    [self.itemTitle setText:[dic objectForKey:@"title"]];
    if([@"in" isEqualToString:[dic objectForKey:@"is_in"]]){
        [_isIn setSelectedSegmentIndex:1];
    }else{
        [_isIn setSelectedSegmentIndex:2];
    }
}
/////////////////////////////////////////////////////////////////////////////////////////////////
-(void)createMode{
    _isCreateMode = YES;
    
}

@end
